function change_background(){
    $("tbody tr").hover(
    function() {
        $(this).css({background:"#EAF8DB"});
    },
    function() {
        $(this).css({background:"#fff"});
    });
}

$(function(){
    change_background();
	
	//$('img[ectype="flex"]').live();
	$('img[ectype="flex"]').live("click", function(){
        var status = $(this).attr("status");
        var id = $(this).attr("fieldid");
        var src = $(this).attr('src');

        //状态是加号的事件
        if(status == 'open') {
            var pr = $(this).parent('td').parent('tr');
            var pid = pr.attr('class');
            var sr  = pr.clone();
            var td2 = sr.find("td:eq(1)");
            td2.prepend("<img class='preimg' src='"+resource+"/images/treetable/vertline.gif'/>")
                            .children('span')
                            .remove().end()
                            .find("img[ectype=flex]").remove();
            var td2html = td2.html();
            //如果已经请求过的数据再次请求时只显示改变状态，不再去请求
            /*if($("."+id).length > 0)
            {
                $("."+id).show();
                $(this).attr('src',src.replace("tv-expandable","tv-collapsable"));
                $(this).attr('status','close');
                return;
            }*/
            $.get(app + "/articlecat/ajax_cat", {id: id}, function(data){
                if (data) {
                    var str = "";
                    var res = eval('('+data+')');
                    for(var i = 0; i < res.length; i++) {
                        var src = "";
                        var if_show = "";
                        //给每一个异步取出的数据添加伸缩图标后者无状态图标
                        if(res[i].hasChild)
                        {
                           src =  "<img src='"+resource+"/images/treetable/tv-expandable.gif' ectype='flex' status='open' fieldid="+res[i].id+">";
                        } else {
                           src =  "<img src='"+resource+"/images/treetable/tv-item.gif' fieldid='"+res[i].id+"'>";
                        }
                        var itd2 = td2html+src;
                        //给每一个取出的数据添加是否显示状态标志
                        if(res[i].status) {
                            if_show = "<img src='"+resource+"/images/positive_enabled.gif' />";
                        } else {
                            if_show = "<img src='"+resource+"/images/positive_disabled.gif' />";
                        }
                        //构造每一个tr组成的字符串，标语添加
                        str+="<tr class='"+pid+" row"+id+"'><td class='align_center w30'><input type='checkbox' class='checkitem' value='"+res[i].id+"' /></td>"+
                        "<td class='node' width='50%'>"+itd2+"<span class='node_name'>"+res[i].name+"</span></td>"+
            "<td class='align_center'><span>"+res[i].sort+"</span></td>"+
            "<td class='align_center'>"+if_show+"</td>"+
            "<td class='handler'><span><a href='"+app+"/articlecat/edit/id/"+res[i].id+"'>编辑</a> | <a href='javascript:if(confirm(\"删除该分类将会同时删除该分类的所有下级分类，您确定要删除吗?\"))window.location=\""+app+"/articlecat/del/id/"+res[i].id+"\";'>删除</a> | <a href='"+app+"/articlecat/add/id/"+res[i].id+"'>新增下级</a></span></td></tr>";
                    }
                    //将组成的字符串添加到点击对象后面
                    pr.after(str);
                    change_background();
                }
            });
            $(this).attr('src',src.replace("tv-expandable","tv-collapsable"));
            $(this).attr('status','close');
        }
        //状态是减号的事件
        if(status == "close")  {
            $('.row'+id).hide();
            $(this).attr('src',src.replace("tv-collapsable","tv-expandable"));
            $(this).attr('status','open');
        }
	});
	
});